<?php

class Search_modul  extends CI_Model {
	public $select;

	public function __construct() {
		// Call the Model constructor
		parent::__construct();
	}
 
	public function poligon($get) {
		 
		if (isset($get['in_polygon'][0])) {
			foreach ($get['in_polygon'] as $key => $value) {
				$polygonArr = explode(",", $value);

				$select_type = " MBRIntersects(GeomFromText('Polygon((";

				$polygonArr_chunk = array_chunk($polygonArr, 2);

				foreach ($polygonArr_chunk as $key => $polygonvalue) {

					$count_type = count($polygonArr_chunk);

					if ($count_type == ($key + 1)) {

						$select_type .= $polygonvalue[0] . ' ' . $polygonvalue[1] . ',' . $polygonArr[0] . ' ' . $polygonArr[1] . " ))'),`points`) ";
					} else {

						$select_type .= $polygonvalue[0] . ' ' . $polygonvalue[1] . ',';
					}

				}
				$select_data[] = $select_type;
			}
			
			
			return $this -> beetwin_select($select_data);
			
		}
		return 0 ;
	}
// За какое время добавлено объявление 
 

public function totime($get)
{
		
	if (isset($get['totime'])) {
		
		
			if ($get['totime'] != '' && $get['totime'] != 0) {
					$t = time() - $get['totime'];
					$dts = "FROM_UNIXTIME('" . $t . "')";
					return	$select_type = "  r.datetime > " . $dts;
			}
	
}

	return 0;
} 
	public function minprice($outget) {
                
                
            $currency['value']['dollars'] = 30;
		############# валюта currency на всех вариантах

		if (isset($outget['currency'])) {
			
 

			############# Стоимость 
			# от
			if (isset($outget['minprice'])) {

				if ($outget['minprice'] != '' && $outget['minprice'] != 0 ) {
                        $select_type = " ( r.price >= '" . ($outget['minprice'] * $currency['value']['dollars']) . "' and r.currency = 1 ) and ( r.price >= '" . $outget['minprice'] . "'  and r.currency = 2 ) ";
				
             
                	return $select_type;
				}
				return 0;
			}
  
		}
		####################################

		return 0;
	}















	public function maxprice($outget) {
 $currency['value']['dollars'] = 30;
		############# валюта currency на всех вариантах

		if (isset($outget['currency'])) { 
			if (isset($outget['maxprice'])) {

				if ($outget['maxprice'] != '') {
						$select_type = " ( r.price <= '" . $outget['maxprice'] * $currency['value']['dollars'] . "' and r.currency = 1 ) and ( r.price <= '" . $outget['maxprice'] . "'  and r.currency = 2 ) ";
			         return $select_type; 
				}
			} 
 
		}

		return 0;
	}












// метро only_foot





	public function only_foot_metro($outget) {

		############# валюта currency на всех вариантах

		if (isset($outget['only_foot']) && isset($outget['only_foot'])) {
				 
				 
		if($outget['only_foot']!=0){
				 
				 
				 
			$select_data[] = " r.goby = '" . $outget['only_foot'] . "' ";
	
	
	
		 #  <option value="1">пешком </option>
         #  <option value="2">транспортом </option>

			if ($outget['only_foot'] == '1' || $outget['only_foot'] == '2') {  

			# от
			if (isset($outget['foot_min2'])) {

				if ($outget['foot_min2'] != '') { 
						$select_data[] = " r.min_user >= '" . $outget['foot_min2'] . "' "; 
			
				}
				return 0;
			}
			# до 
			if (isset($outget['foot_min'])) {

				if ($outget['foot_min'] != '') { 
						$select_data[] = " r.min_user <= '" . $outget['foot_min'] . "' ";
				}
			}
			
			
			
			
			
			}
			
			return $this->beetwin_select($select_data); 
		}	
	}
		#################################### 
		return 0;
	}


























 
	public function region($outget) {
		############# регион

		if (isset($outget['obl_id'])) {

			if ($outget['obl_id'] == '7700000000000' || $outget['obl_id'] == '7800000000000') {
				$select_type = ' r.city_id =  \'' . $outget['obl_id'] . "'";
			} else {
				$select_type = ' r.region_id =  \'' . $outget['obl_id'] . "'";
			}

			return $select_type;

		}
		return 0;
	}
/*
	// тип недвижимости, дом, участок
	public function object_type($get) {

		if (isset($get['object_type']['0'])) {
			
			if ($get['object_type']['0'] != '') {
	
				$count_type = count($get['object_type']);
				$select_type = ' r.object_type IN ( ';
	
				#     grp.id IN( 1,2,3 )
	
				foreach ($get['object_type'] as $key => $value) {
	
					if ($count_type == ($key + 1)) {
	
						$select_type .= $value . ') ';
	
					} else {
						$select_type .= $value . ',';
					}
				}
				$select_data = $select_type;
	
				return $select_data;
	
			}
		}
		return 0;
	}
	
	*/
	
	
	
	
	// тип недвижимости, дом, участок
	public function object_type($get ='') {

		if (isset($get['object_type']['0'])) {
	  
			
			if ($get['object_type']['0'] != '' && $get['object_type']['0'] != 0) {
	
				$count_type = count($get['object_type']);
				$select_type = ' r.object_type IN ( ';
	
				#     grp.id IN( 1,2,3 )
	 
	
				foreach ($get['object_type'] as $key => $value) {
 
					if ($count_type == ($key + 1)) {
	
						$select_type .= $value . ') ';
	
					} else {
						$select_type .= $value . ',';
					}


				}
				
				
				
				
 
				return $select_type;
	
			}
		}
		return 0;
	}
	
	
	

	// Общая площадь миниум
	public function total_minarea($get) {

		if (isset($get['minarea'])) {
			if ($get['minarea'] != '') {
				return " r.area >= '" . $get['minarea'] . "' ";
			}
		}
		return 0;
	}

	public function total_maxarea($get) {
			if (isset($get['maxarea'])) {
		if ($get['maxarea'] != '') {
		 
				return " r.area <= '" . $get['maxarea'] . "' ";
			}
		}
		return 0;
	}
	
	
	
	
	
	
	
	
	
	
	
	
		
	
 
	
	
	
	
	
	
	
	// Площадь Жилая  
	public function minlarea($get) {

		if (isset($get['minlarea'])) {
			if ($get['minlarea'] != '') {
				return " r.l_area >= '" . $get['minlarea'] . "' ";
			}
		}
		return 0;
	}

	public function maxlarea($get) {
				if (isset($get['maxlarea'])) {
		if ($get['maxlarea'] != '') {
			if (isset($get['maxlarea'])) {
				return " r.l_area <= '" . $get['maxlarea'] . "' ";
			}
		}
		}
		return 0;
	}


	
	// Площадь Жилая  
	public function mintarea($get) {

		if (isset($get['minarea'])) {
			if ($get['minarea'] != '') {
				return " r.t_area >= '" . $get['minarea'] . "' ";
			}
		}
		return 0;
	}

	public function maxtarea($get) {
		
		if (isset($get['maxarea'])) {
		if ($get['maxarea'] != '') {
			if (isset($get['maxarea'])) {
				return " r.t_area <= '" . $get['maxarea'] . "' ";
			}
		}	}
		
		
		return 0;
	}


	
	// Площадь кухни kitchen
	public function minkarea($get) {

		if (isset($get['minkarea'])) {
			if ($get['minkarea'] != '') {
				return " r.kitchen >= '" . $get['minkarea'] . "' ";
			}
		}
		return 0;
	}

	public function maxkarea($get) {
		if (isset($get['maxkarea'])) {
		if ($get['maxkarea'] != '') {
			if (isset($get['maxkarea'])) {
				return " r.kitchen <= '" . $get['maxkarea'] . "' ";
			}
		}
		}
		return 0;
	}
	
	
	 


 
 

















	// Общая площадь участка
	public function minsite($get) {
		if (isset($get['minsite'])) {

			if ($get['minsite'] != '') {
				return " r.site >= '" . $get['minsite'] . "' ";
			}
		}
		return 0;
	}

	public function maxsite($get) {

		if (isset($get['maxsite'])) {
			if ($get['maxsite'] != '') {
				return " r.site <= '" . $get['maxsite'] . "' ";
			}
		}
		return 0;
	}

	
	
	
	
	// Общая площадь участка
	public function object_type2($get) {
		if (isset($get['object_type2'])) {

			if ($get['object_type2'] == '1' || $get['object_type2'] == '2') {
				return " r.object_type2 = '" . $get['object_type2'] . "' ";
			}
		}
		return 0;
	}
	 
	
	// Общая площадь участка
	public function sost_type($get) {
		if (isset($get['sost_type'])) {

			if ($get['sost_type'] == '1' || $get['sost_type'] == '2') {
				return " r.sost_type = '" . $get['sost_type'] . "' ";
			}
		}
		return 0;
	}
	 
	
	 
	 
	
	// вторичка или новостро1
	public function object_type3($get) {
		if (isset($get['object_type3'])) {

			if ($get['object_type3'] == '1' || $get['object_type3'] == '2') {
				return " r.object_type2 = '" . $get['object_type3'] . "' ";
			}
		}
		return 0;
	}
	 
	// Общая площадь участка
	public function ipoteka($get) {
		if (isset($get['ipoteka'])) {

			if ($get['ipoteka'] == '1') {
				return " r.ipoteka = '1' ";
			}
		}
		return 0;
	}
	 
	 
	 
	
	
	
	
	
	
	
	// Колличество комнат
	public function minroom($get) {
		if (isset($get['minroom'])) {
			if ($get['minroom'] != '') {
				return " r.room >= '" . $get['minroom'] . "' ";
			}
		}
		return 0;
	}








	public function maxroom($get) {
		if (isset($get['maxroom'])) {
			if ($get['maxroom'] != '') {
				return " r.room <= '" . $get['maxroom'] . "' ";
			}
		}
		return 0;
	}
	
	
	
	// Этажность этаж
	public function floor($get) {
		if (isset($get['floor'])) {
			if ($get['floor'] != '') {
				return " r.floor >= '" . $get['floor'] . "' ";
			}
		}
		return 0;
	}



	public function floorn($get) {
		if (isset($get['floorn'])) {
			if ($get['floorn'] != '') {
				return " r.floor <= '" . $get['floorn'] . "' ";
			}
		}
		return 0;
	}
	 
	
	 
	
	
	
	
	
	
	
	
		// Этажность квартиры
	public function minfloor($get) {
		if (isset($get['minfloor'])) {
			if ($get['minfloor'] != '') {
				return " r.floor >= '" . $get['minfloor'] . "' ";
			}
		}
		return 0;
	}


	
		// Этажность квартиры
	public function maxfloor($get) {
		if (isset($get['maxfloor'])) {
			if ($get['maxfloor'] != '') {
				return " r.floor <= '" . $get['maxfloor'] . "' ";
			}
		}
		return 0;
	}





// не последний этаж квартиры
	public function floornl($get) {
		if (isset($get['floornl'])) {
			if ($get['floornl'] = '1') {
				return " r.floor != r.floorn ";
			}
		}
		return 0;
	}
	
	
	
	
	
		// Этажность дома
	public function minfloorn($get) {
		if (isset($get['minfloorn'])) {
			if ($get['minfloorn'] != '') {
				return " r.floorn >= '" . $get['minfloorn'] . "' ";
			}
		}
		return 0;
	}
 
	public function maxfloorn($get) {
		if (isset($get['maxfloorn'])) {
			if ($get['maxfloorn'] != '') {
				return " r.floorn <= '" . $get['maxfloorn'] . "' ";
			}
		}
		return 0;
	}
	
	
	public function lift($get) {
		if (isset($get['minlift'])) {
			if ($get['minlift'] != '') {
				return " r.lift >= '" . $get['minlift'] . "' ";
			}
		}
		return 0;
	}
	
	
	 
	
	
	
	
	
	// Мебель
	public function mebel($get) {
		if (isset($get['mebel'])) {
			if ($get['mebel'] == '1') {
				return " r.mebel = '1' ";
			}
		}
		
		if (isset($get['mebel'])) {
			if ($get['mebel'] == '-1') {
				return " r.mebel is not null ";
			}
		}
		return 0;
	}
	
	
	
	
	// Мебель на кухне
	public function mebel_k($get) {
		if (isset($get['mebel_k'])) {
			if ($get['mebel_k'] == '1') {
				return " r.mebel_k = '1' ";
			}
		}
		
		if (isset($get['mebel_k'])) {
			if ($get['mebel_k'] == '-1') {
				return " r.mebel_k is not null ";
			}
		}
		return 0;
	}
	
	
	
	
	// телефон
	public function phone($get) {
		if (isset($get['phone'])) {
			if ($get['phone'] == '1') {
				return " r.phone = '1' ";
			}
		}
		
		if (isset($get['phone'])) {
			if ($get['phone'] == '-1') {
				return " r.phone is not null ";
			}
		}
		return 0;
	}
	
	
	
	// Телевизор
	public function tv($get) {
		if (isset($get['tv'])) {
			if ($get['tv'] == '1') {
				return " r.tv = '1' ";
			}
		}
		
		if (isset($get['tv'])) {
			if ($get['tv'] == '-1') {
				return " r.tv is not null ";
			}
		}
		return 0;
	}
	
	// Холодильник
	public function rfgr($get) {
		if (isset($get['rfgr'])) {
			if ($get['rfgr'] == '1') {
				return " r.rfgr = '1' ";
			}
		}
		
		if (isset($get['rfgr'])) {
			if ($get['rfgr'] == '-1') {
				return " r.rfgr is not null ";
			}
		}
		return 0;
	}
	
	// Стиральная машина
	public function wm($get) {
		if (isset($get['wm'])) {
			if ($get['wm'] == '1') {
				return " r.wm = '1' ";
			}
		}
		
		if (isset($get['wm'])) {
			if ($get['wm'] == '-1') {
				return " r.wm is not null ";
			}
		}
		return 0;
	}
	
	 
	
	// Балкон
	public function balkon($get) {
		if (isset($get['balkon'])) {
			if ($get['balkon'] == '1') {
				return " r.balkon = '1' ";
			}
		}
		return 0;
	}
	
	
	// можно животных:
	public function pet($get) {
		if (isset($get['pet'])) {
			if ($get['pet'] == '1') {
				return " r.pet = '1' ";
			}
		}
		 
		return 0;
	}
	
	// можно с детьми
	public function kids($get) {
		if (isset($get['kids'])) {
			if ($get['kids'] == '1') {
				return " r.kids = '1' ";
			}
		} 
		return 0;
	}
	
	 
		// Мебель в арендуемом помещении
	 
	public function rent_office_mebel($get) {
		if (isset($get['mebel'])) {
			if ($get['mebel'] == '3') {
				return " r.mebel = '1' ";
			}
			if ($get['mebel'] == '2') {
				return " r.mebel IS NOT NULL  ";
			}
		} 
		return 0;
	}
	
		// свободный вход 
	 
	public function rent_office_enter($get) {
		if (isset($get['enter'])) {
			if ($get['enter'] == '3') {
				return " r.enter = '1' ";
			}
			if ($get['enter'] == '2') {
				return " r.enter IS NOT NULL  ";
			}
		} 
		return 0;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	 
	
	
	// срок предоплаты
	public function for_days($get) {
		if (isset($get['maxprepay'])) { 
			if ($get['maxprepay'] >= '1' && $get['maxprepay'] <= '12') {
				return " r.for_days = '" . $get['type'] . "' ";
			}
		}
		return 0;
	}
	
	
	 
	
	
	
	
	
	
	
	// кол. телефонов
	public function minphone($get) {
		if (isset($get['minphone'])) {
			if ($get['minphone'] != '') {
				return " r.phone <= '" . $get['minphone'] . "' ";
			}
		}
		return 0;
	}
	// можно ли ещё телефоны телефонов
	public function add_phone($get) {
		if (isset($get['add_phone'])) {
			if ($get['add_phone'] != '') {
				return " r.phone = '" . $get['add_phone'] . "' ";
			}
		}
		return 0;
	}
	 
	// Что из себя представляет вход
	public function enter($get) {
		if (isset($get['enter'])) {
			if ($get['enter'] == '1' || $get['enter'] == '2') {
				return " r.enter = '" . $get['enter'] . "' ";
			}
		}
		return 0;
	}
	
	
	
	
	
	
	// Что из себя представляет вход
	public function contract($get) {
				if (isset($get['contract']['0'])) {

			$count_type = count($get['contract']);
			$select_type = ' r.contract IN ( ';
 	

			foreach ($get['contract'] as $key => $value) {

				if ($count_type == ($key + 1)) {

					$select_type .= $value . ') ';

				} else {
					$select_type .= $value . ',';
				}
			}
			$select_data = $select_type;

			return $select_data;

		}
		return 0;
	}



	// колличество комнат
	public function room($get) {
				if (isset($get['room']['0'])) {

			$count_type = count($get['room']);
			$select_type = ' r.room IN ( ';
 	

			foreach ($get['room'] as $key => $value) {

				if ($count_type == ($key + 1)) {

					$select_type .= $value . ') ';

				} else {
					$select_type .= $value . ',';
				}
			}
			$select_data = $select_type;

			return $select_data;

		}
		return 0;
	}
	 
	 
	 
	 
	 
	 
	// колличество комнат
	public function room_root($get) {
				if (isset($get['room']['0'])) {

			$count_type = count($get['room']);
			$select_type = ' r.root IN ( ';
 	

			foreach ($get['room'] as $key => $value) {

				if ($count_type == ($key + 1)) {

					$select_type .= $value . ') ';

				} else {
					$select_type .= $value . ',';
				}
			}
			$select_data = $select_type;

			return $select_data;

		}
		return 0;
	}
	 
	 
	 
	 
	 
	 
	// колличество комнат
	public function type_room($get) {
				if (isset($get['room']['0'])) {

			$count_type = count($get['room']);
			$select_type = ' r.room IN ( ';
 	

			foreach ($get['room'] as $key => $value) {

				if ($count_type == ($key + 1)) {

					$select_type .= $value . ') ';

				} else {
					$select_type .= $value . ',';
				}
			}
			$select_data = $select_type;

			return $select_data;

		}
		return 0;
	}
	 
	
	   
	 
	 
	 
	 
	
	
	 
	// жилое не жилое продажа офиса
	public function b_type($get) {

		if (isset($get['b_type']['0'])) {

			$count_type = count($get['b_type']);
			$select_type = ' r.building IN ( ';

			#     grp.id IN( 1,2,3 )

			foreach ($get['b_type'] as $key => $value) {

				if ($count_type == ($key + 1)) {

					$select_type .= $value . ') ';

				} else {
					$select_type .= $value . ',';
				}
			}
			$select_data = $select_type;

			return $select_data;

		}
		return 0;
	}
	
	
	 
		// фото обязательно
	public function onlyfoto($get) {
		if (isset($get['foto'])) {
			if ($get['foto'] != '') {
				return " i.id IS NOT NULL ";
			}
		}
		return 0;
	}
		// Метро
	public function metro($get) {
		
		if(isset($get['m'])){
		
				if ($get['m'] != '') {

				$MetroArr = explode("_", $get['m']);

				$count_type = count($MetroArr);
				$select_type = ' r.id_metro IN ( ';

				foreach ($MetroArr as $key => $value) {

					if ($count_type == ($key + 1)) {

						$select_type .= $value . ') ';

					} else {
						$select_type .= $value . ','; 
					} 
				} 
				return $select_type;
			
				
				
				} 
		}  
		return 0;
	}
	
 
	  
	
	
	
	// Общая площадь участка
	public function land_type($get) {

		if (isset($get['land_type']['0'])) {

			$count_type = count($get['land_type']);
			$select_type = ' r.land_type IN ( ';

			#     grp.id IN( 1,2,3 )

			foreach ($get['land_type'] as $key => $value) {

				if ($count_type == ($key + 1)) {

					$select_type .= $value . ') ';

				} else {
					$select_type .= $value . ',';
				}
			}

			return $select_type;

		}
		return 0;
	}

	############# Улица
	public function street($outget) {

		if (isset($outget['streets']['0'])) {
			$count_type = count($outget['streets']);
			if ($outget['r'] == '7700000000000' || $outget['r'] == '7800000000000') {
				$select_type = ' r.street_id IN ( ';
			} else {
				$select_type = ' r.city_id IN ( ';
			}

			foreach ($outget['streets'] as $key => $value) {
				if ($count_type == ($key + 1)) {
					$select_type .= $value . ') ';
				} else {
					$select_type .= $value . ',';
				}

			}
			return $select_type;
		}
		return 0;
	}

	public function beetwin_select($select_data = '') {
		$select = '';
		# составление запроса
		$co = count($select_data);
		foreach ($select_data as $key => $value) {
			if ($co == ($key + 1)) {
				$select .= ' ' . $value . ' ';
			} else {

				$select .= ' ' . $value . ' AND ';

			}

		}
		return $select;
	}


 
	public function end_select($select_data = '11111') {
 
		$select = ''; 
		foreach ($select_data as $key => $value_n) {
			#  эдаляем пыстые массивы
			if ($value_n != '0') {  
				$sexet[] = $value_n; 
			}
		}
 
  if(isset($sexet[0])){
		$co = count($sexet);
	  
		if ($co > 0) {
			foreach ($sexet as $key => $value) {
				
				if ($key == 0) {
					$select = 'WHERE ';
				} 
				 $value.'    ';
				
				if ($co == ($key + 1)) {
					$select .= ' ' . $value . ' ';
				} else {
					$select .= ' ' . $value . ' AND ';
				}
				
				
			} 
			return $select;
		}
		 
		}
  return false;
	}
}
